const Service = require('egg').Service;
const { Sequelize, Op } = require('sequelize');

class SecretaryService extends Service {
    // 添加用service
    async add(secretary) {
        const { ctx } = this;
        const result = await ctx.model.Secretary.create(secretary);
        return result;
    }
    // 分页查询用service
    async findList(keywords, pageIndex, pageSize) {
        const { ctx } = this;
        const result = await ctx.model.Secretary.findAndCountAll({
            where: {
                name: {
                    [Op.like]: '%' + keywords + '%'
                }
            },
            offset: (pageIndex - 1) * pageSize,
            limit: pageSize
        });
        return result;
    }
    // 根据id查找对应的值
    async findById(id) {
        const { ctx } = this;
        const result = await ctx.model.Secretary.findByPk(id);
        return result;
    }
    // 修改
    async modify(secretary) {
        const { ctx } = this;
        const { 
            id,
            name,
            gender,
            telephone,
            picture,
        } = secretary;
        const result = await ctx.model.Secretary.update({
            name,
            gender,
            telephone,
            picture,
        }, {
            where: { id }
        });
        return result;
    }
    // 删除
    async remove(id) {
        const { ctx } = this;
        const result = await ctx.model.Secretary.update({
            deleted:1
        },{
            where: { id }
        });
        return result;
    }
}
module.exports = SecretaryService;